package resolver

// This file will be automatically regenerated based on the schema, any resolver implementations
// will be copied through when generating and any unknown code will be moved to the end.

import (
	"context"
	"errors"
	"fmt"

	"mall/service/layout/rpc/layout"
	"mall/service/wpgql/api/internal/graph/generated"
	"mall/service/wpgql/api/internal/graph/helper"
	"mall/service/wpgql/api/internal/graph/model"
)

const blogLayoutMenuSingleError = "could not get blog menu"

func (r *menuResolver) MenuItems(
	ctx context.Context,
	obj *model.Menu,
	first *int,
	last *int,
	after *string,
	before *string,
	where *model.MenuToMenuItemConnectionWhereArgs,
) (*model.MenuToMenuItemConnection, error) {
	if where == nil {
		return obj.MenuItems, nil
	}

	return &model.MenuToMenuItemConnection{}, nil
}

func (r *queryResolver) Menu(ctx context.Context, id string, idType *model.MenuNodeIDTypeEnum) (*model.Menu, error) {
	req := &layout.MenuDetailRequest{}

	preload, eagerLoad := helper.GetBlogLayoutMenuPreloads(ctx)
	if preload != "" {
		req.Preload = preload
	}
	if eagerLoad != "" {
		req.EagerLoad = eagerLoad
	}
	filter := helper.GetBlogLayoutMenuFilter(id, idType)
	if filter != "" {
		req.Filter = filter
	}

	res, err := r.svcCtx.LayoutRpc.MenuDetail(ctx, req)
	if err != nil {
		return nil, errors.New(blogLayoutMenuSingleError)
	}

	return helper.BlogLayoutMenuToGraphQL(res), nil
}

func (r *queryResolver) Menus(ctx context.Context, first *int, last *int, after *string, before *string, where *model.RootQueryToMenuConnectionWhereArgs) (*model.RootQueryToMenuConnection, error) {
	panic(fmt.Errorf("not implemented"))
}

// Menu returns generated.MenuResolver implementation.
func (r *Resolver) Menu() generated.MenuResolver { return &menuResolver{r} }

type menuResolver struct{ *Resolver }
